<? 
$pageTitle = "Add New Locations";
require("includes/functions.php"); 

if (!empty($submit)) {
    // Convert strings

	$loc_name     = fieldToDB($loc_name);
	$loc_address1 = fieldToDB($loc_address1);
	if (!empty($loc_address2)) { $loc_address2 = fieldToDB($loc_address2); }
	$loc_city     = fieldToDB($loc_city);
	$loc_hours    = fieldToDB($loc_hours);
	$loc_access   = fieldToDB($loc_access);

	$contact1_title = fieldToDB($contact1_title);
	$contact2_title = fieldToDB($contact2_title);

	// Enter contacts first
	if ( (!empty($contact1_last) && ($contact1_last != "Last")) || (!empty($contact1_phone)) ) {
	    $sql = "INSERT into oa_btb_Contacts (
				GI_NamePrefix,
				GI_NameFirst,
				GI_NameLast,
				GI_NameSuffix,
				GI_ProfessionalTitle,
				CI_PhoneNumber,
				CI_EmailAddress,
				IsVisible,
				AddDate
				) values (
	            '$contact1_pre',
	            '$contact1_first',
	            '$contact1_last',
	            '$contact1_suffix',
	            '$contact1_title',
	            '$contact1_phone',
	            '$contact1_email',
	            '0',
	            '$date'
	            )";
         $result = mysql_db_query($dbname,$sql);

         $sql = "SELECT MAX(Contact_PID) from oa_btb_Contacts";
         $result = mysql_db_query($dbname,$sql);
         $row = mysql_fetch_array($result);
         $contact1_ID = $row[0];
	}
	if ( (!empty($contact2_last) && ($contact2_last != "Last")) || (!empty($contact2_phone)) ) {
        $sql = "INSERT into oa_btb_Contacts (
				GI_NamePrefix,
				GI_NameFirst,
				GI_NameLast,
				GI_NameSuffix,
				GI_ProfessionalTitle,
				CI_PhoneNumber,
				CI_EmailAddress,
				IsVisible,
				AddDate
				) values (
	            '$contact2_pre',
	            '$contact2_first',
	            '$contact2_last',
	            '$contact2_suffix',
	            '$contact2_title',
	            '$contact2_phone',
	            '$contact2_email',
	            '0',
	            '$date'
	            )";
         $result = mysql_db_query($dbname,$sql);

         $sql = "SELECT MAX(Contact_PID) from oa_btb_Contacts";
         $result = mysql_db_query($dbname,$sql);
         $row = mysql_fetch_array($result);
         $contact2_ID = $row[0];
	}

	$sql = "INSERT into oa_btb_Sites (
				Provider_ID,
				SiteName,
				SiteStreet1,
				SiteStreet2,
				SiteCity,
				SiteStateAbbr,
				SiteZipCode,
				SitePhoneMain,
				SitePhoneFree,
				SitePhoneFax,
				SitePhoneTTY,
				SiteEmailGeneral,
				SiteContact1_ID,
				SiteContact2_ID,
				SiteAccessibility,
				SitePublicHours,
				IsVisible,
				AddDate
				) values (
				'$PID',
				'$loc_name',
				'$loc_address1',
				'$loc_address2',
				'$loc_city',
				'$loc_state',
				'$loc_zip',
				'$loc_phone1',
				'$loc_phone2',
				'$loc_phone4',
				'$loc_phone3',
				'$loc_email',
				'$contact1_ID',
				'$contact2_ID',
				'$loc_access',
				'$loc_hours',
				'0',
				'$date'
				)";
	$result = mysql_db_query($dbname,$sql);
	
	// Update master record modification information
	$sql = "UPDATE oa_btb_Providers SET
			ModDate          = '$date',
			ModUID           = '$UID'
			
		WHERE
			Provider_PID = '$PID'";
	$result = mysql_db_query($dbname,$sql);	
}

$loc_access = DBtoField($loc_access);
$loc_hours  = DBtoField($loc_hours);

// Get information for this provider 
$sql = "SELECT ProviderNameFull from oa_btb_Providers WHERE Provider_PID = '$PID'";
$result = mysql_db_query($dbname,$sql);
$row = mysql_fetch_assoc($result);
$org_name    = $row[ProviderNameFull];

require("includes/header_popup.php"); ?>

<Script Language="JavaScript">
<!--
function checkForm(form) {
	if (form.loc_name.value == "") {
		alert("Please enter a Location Name.")
		return false
	}
	if (form.loc_address1.value == "") {
		alert("Please enter an Address for this location.")
		return false
	}
	if (form.loc_city.value == "") {
	    alert("Please enter a City for this location.")
		return false
	}
	
	if (form.loc_phone1.value == "") {
		alert("Please enter a Phone Number for this location.")
		return false
	}
	if (form.loc_zip.value == "") {
		alert("Please enter a Zip Code for this location.")
		return false
	}
    if(form.loc_hours.value.length>=250) {
          alert('Hours of Operation must be 250 characters or less.');
          form.loc_hours.focus();
          return false;
    }
    if(form.loc_access.value.length>=250) {
          alert('Location Accessibility must be 250 characters or less.');
          form.loc_access.focus();
          return false;
    }

	return true
}
//-->
</script>

<div id="datatablePopup">
<b>Updating: <?=$org_name; ?></b>
<form action="<?=$PHP_SELF; ?>?submit=1" method="POST" name="main" onSubmit="return checkForm(this)">
	<input type="hidden" name="date" value="<? echo date("Y-m-d"); ?>">
	<input type="hidden" name="PID" value="<?=$PID; ?>">
	
	<table border="0" width="100%" cellspacing="3">
	<tr>
		<td >Location Name <font color="red"><b>&raquo;</b></font></td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_name" size="30" value="<?=$loc_name; ?>"></td>
	</tr>
	<tr>
		<td >Address Line 1 <font color="red"><b>&raquo;</b></font></td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_address1" size="30" value="<?=$loc_address1; ?>"></td>
	</tr>
	<tr>
		<td >Address Line 2</td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_address2" size="30" value="<?=$loc_address2; ?>"></td>
	</tr>
	<tr>
		<td >City <font color="red"><b>&raquo;</b></font></td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_city" size="30" value="<?=$loc_city; ?>"></td>
	</tr>
	<tr>
		<td >State &amp; Zip Code <font color="red"><b>&raquo;</b></font></td>
		<td bgcolor="#eeeeee">
			<select name="loc_state" size="1">
			<? if (!empty($loc_state)) {
			    echo "<option value=\"$loc_state\" selected>$loc_state</option>\n";
			} ?>
			<option value=
			<? include("includes/states.html"); ?>
			</select>
			<input type="text" name="loc_zip" size="10" value="<?=$loc_zip; ?>"></td>
	</tr>
	<tr>
		<td >Main Phone <font color="red"><b>&raquo;</b></font></td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_phone1" size="30" value="<?=$loc_phone1; ?>"></td>
	</tr>
	<tr>
		<td >Toll Free</td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_phone2" size="30" value="<?=$loc_phone2; ?>"></td>
	</tr>
	<tr>
		<td >TTY Phone</td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_phone3" size="30" value="<?=$loc_phone3; ?>"></td>
	</tr>
	<tr>
		<td >Fax Number</td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_phone4" size="30" value="<?=$loc_phone4; ?>"></td>
	</tr>
	<tr>
		<td >Primary Email Address</td>
		<td bgcolor="#eeeeee"><input type="text" name="loc_email" size="40" value="<?=$loc_email; ?>"></td>
	</tr>
	<tr>
		<td  valign="top">Hours of Operation<br>
            <span class="subText">[250 characters max]</span>
		</td>
		<td bgcolor="#eeeeee">
			<? echo "<textarea name=\"loc_hours\" rows=\"3\" cols=\"55\">$loc_hours</textarea>\n"; ?>
		</td>
	</tr>
	<tr>
		<td  valign="top">Location Accessibility<br>
			<span class="subText">[250 characters max]</span>
		</td>
		<td bgcolor="#eeeeee">
			<? echo "<textarea name=\"loc_access\" rows=\"3\" cols=\"55\">$loc_access</textarea>\n"; ?>
		</td>
	</tr>
	<tr>
		<td bgcolor="#666666"><b><font color="white">Enter Location Contacts</font></b></td>
		<td bgcolor="#666666"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Name</td>
		<td bgcolor="#eeeeee">
			<input type="text" name="contact1_pre" size="8"
			<? if (!empty($contact1_pre)) { echo " value=\"$contact1_pre\""; }
			else {
			    echo " value=\"Prefix\" onFocus=\"document.main.contact1_pre.value=''\";";
			}
			?>
			>
			<input type="text" name="contact1_first" size="12"
            <? if (!empty($contact1_first)) { echo " value=\"$contact1_first\""; }
			else {
			    echo " value=\"First\" onFocus=\"document.main.contact1_first.value=''\";";
			}
			?>
			>
			<input type="text" name="contact1_last" size="12"
            <? if (!empty($contact1_last)) { echo " value=\"$contact1_last\""; }
			else {
			    echo " value=\"Last\" onFocus=\"document.main.contact1_last.value=''\";";
			}
			?>
			>
			<input type="text" name="contact1_suffix" size="8"
            <? if (!empty($contact1_suffix)) { echo " value=\"$contact1_suffix\""; }
			else {
			    echo " value=\"Suffix\" onFocus=\"document.main.contact1_suffix.value=''\";";
			}
			?>
			>
		</td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Title</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact1_title" value="<?=$contact1_title; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Phone</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact1_phone" value="<?=$contact1_phone; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Email</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact1_email" value="<?=$contact1_email; ?>" size="30"></td>
	</tr>

	<tr>
		<td  valign="top">Contact #2 Name</td>
		<td bgcolor="#eeeeee">
			<input type="text" name="contact2_pre" size="8"
			<? if (!empty($contact2_pre)) { echo " value=\"$contact2_pre\""; }
			else {
			    echo " value=\"Prefix\" onFocus=\"document.main.contact2_pre.value=''\";";
			}
			?>
			>
			<input type="text" name="contact2_first" size="12"
            <? if (!empty($contact2_first)) { echo " value=\"$contact2_first\""; }
			else {
			    echo " value=\"First\" onFocus=\"document.main.contact2_first.value=''\";";
			}
			?>
			>
			<input type="text" name="contact2_last" size="12"
            <? if (!empty($contact2_last)) { echo " value=\"$contact2_last\""; }
			else {
			    echo " value=\"Last\" onFocus=\"document.main.contact2_last.value=''\";";
			}
			?>
			>
			<input type="text" name="contact2_suffix" size="8"
            <? if (!empty($contact2_suffix)) { echo " value=\"$contact2_suffix\""; }
			else {
			    echo " value=\"Suffix\" onFocus=\"document.main.contact2_suffix.value=''\";";
			}
			?>
			>
		</td>
	</tr>
	<tr>
		<td  valign="top">Contact #2 Title</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact2_title" value="<?=$contact2_title; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #2 Phone</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact2_phone" value="<?=$contact2_phone; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #2 Email</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact2_email" value="<?=$contact2_email; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top"></td>
		<td valign="top" align="right"><input type="image" src="images/save.gif" alt="Save"></td>
	</tr>
	</table>
	
	</form>
	</div>
</div>
<div id="footer"></div>
</body>
</html>olor="#eeeeee">
			<input type="text" name="contact1_pre" size="8"
			<? if (!empty($contact1_pre)) { echo " value=\"$contact1_pre\""; }
			else {
			    echo " value=\"Prefix\" onFocus=\"document.main.contact1_pre.value=''\";";
			}
			?>
			>
			<input type="text" name="contact1_first" size="12"
            <? if (!empty($contact1_first)) { echo " value=\"$contact1_first\""; }
			else {
			    echo " value=\"First\" onFocus=\"document.main.contact1_first.value=''\";";
			}
			?>
			>
			<input type="text" name="contact1_last" size="12"
            <? if (!empty($contact1_last)) { echo " value=\"$contact1_last\""; }
			else {
			    echo " value=\"Last\" onFocus=\"document.main.contact1_last.value=''\";";
			}
			?>
			>
			<input type="text" name="contact1_suffix" size="8"
            <? if (!empty($contact1_suffix)) { echo " value=\"$contact1_suffix\""; }
			else {
			    echo " value=\"Suffix\" onFocus=\"document.main.contact1_suffix.value=''\";";
			}
			?>
			>
		</td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Title</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact1_title" value="<?=$contact1_title; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Phone</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact1_phone" value="<?=$contact1_phone; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #1 Email</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact1_email" value="<?=$contact1_email; ?>" size="30"></td>
	</tr>

	<tr>
		<td  valign="top">Contact #2 Name</td>
		<td bgcolor="#eeeeee">
			<input type="text" name="contact2_pre" size="8"
			<? if (!empty($contact2_pre)) { echo " value=\"$contact2_pre\""; }
			else {
			    echo " value=\"Prefix\" onFocus=\"document.main.contact2_pre.value=''\";";
			}
			?>
			>
			<input type="text" name="contact2_first" size="12"
            <? if (!empty($contact2_first)) { echo " value=\"$contact2_first\""; }
			else {
			    echo " value=\"First\" onFocus=\"document.main.contact2_first.value=''\";";
			}
			?>
			>
			<input type="text" name="contact2_last" size="12"
            <? if (!empty($contact2_last)) { echo " value=\"$contact2_last\""; }
			else {
			    echo " value=\"Last\" onFocus=\"document.main.contact2_last.value=''\";";
			}
			?>
			>
			<input type="text" name="contact2_suffix" size="8"
            <? if (!empty($contact2_suffix)) { echo " value=\"$contact2_suffix\""; }
			else {
			    echo " value=\"Suffix\" onFocus=\"document.main.contact2_suffix.value=''\";";
			}
			?>
			>
		</td>
	</tr>
	<tr>
		<td  valign="top">Contact #2 Title</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact2_title" value="<?=$contact2_title; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #2 Phone</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact2_phone" value="<?=$contact2_phone; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top">Contact #2 Email</td>
		<td bgcolor="#eeeeee"><input type="text" name="contact2_email" value="<?=$contact2_email; ?>" size="30"></td>
	</tr>
	<tr>
		<td  valign="top"></td>
		<td valign="top" align="right"><input type="image" src="images/save.gif" alt="Save"></td>
	</tr>
	</table>
	
	</form>
	</div>
</div>
<div id="footer"></div>
</body>
</html>
